import { get } from "lodash";
import axios from "axios";
import utils from "@utils";
import Api from "./Api";
import { UserData } from "@type";

// 登录请求
class LoginApi extends Api {
  constructor() {
    super();
  }

  //发送登录验证吗
  async send_sms(phone: any, type: number = 1) {
    return this._post(`/auth/sendSMS?phone=${phone}&type=${type}`);
  }

  // 短信验证码登录
  async sms_login(phone: string, code: string) {
    const result = await this._post("/auth/loginByPhone", { phone, code });
    return result;
  }

  // 退出登录
  loginOut() {
    return this._post(`/auth/logout`);
  }

  // 获取用户信息
  async get_user_info() {
    const headers = {
      "Content-Type": "application/x-www-form-urlencoded",
    };
    if (localStorage.token) {
      Object.assign(headers, {
        Authorization: `Bearer ${localStorage.token}`,
      });
    }

    return axios
      .create({
        baseURL: utils.isDev ? this.devBaseURL : this.baseURL,
        timeout: this.timeout,
        headers: headers,
        withCredentials: this.withCredentials,
      })
      .get("/auth/getUserInfo");
  }

  //修改用户信息
  updateUser(userData: Partial<UserData>) {
    return this._post("/auth/user/update", userData);
  }
  //验证原手机
  checkPhone(data: { phone: string; code: string }) {
    return this._post("/auth/user/checkPhone", data);
  }
  //修改手机号
  updatePhone(data: { phone: string; code: string }) {
    return this._post("/auth/user/updatePhone", data);
  }

  // 获取accesstoken wxLogin
  wx_Login_get_access_token(params: any) {
    return this._post("/wx_Login_get_access_token", { ...params });
  }
}

const loginApi = new LoginApi();

export default loginApi;
